package com.tenbit.emp.ctl;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.tenbit.emp.svc.EMP1000SVC;
import com.tenbit.emp.svc.EMP1010SVC;
import com.tenbit.emp.vo.EMP1000VO;
import com.tenbit.emp.vo.EMP1010VO;

@Controller
public class EMP1010CTL {
	@Autowired
	private EMP1010SVC emp1010svc;
	@Autowired
	private EMP1000SVC emp1000svc;
	
	public ModelAndView doInitMain(){
		ModelAndView mv=new ModelAndView();
		List<EMP1000VO> sex_code=new ArrayList<EMP1000VO>();
		List<EMP1000VO> dept_code=new ArrayList<EMP1000VO>();
		List<EMP1000VO> skill_code=new ArrayList<EMP1000VO>();
		List<EMP1000VO> academic_code=new ArrayList<EMP1000VO>();
		
		mv.setViewName("EMP1000M0");
		mv.addObject("listZip", emp1000svc.getCodeZipByMvList());
		
		for (EMP1000VO zip : emp1000svc.getCodeZipByMvList()) {
			if(zip.getCode_type().equals("SEX")){
				sex_code.add(zip);
			}else if(zip.getCode_type().equals("DEPT")){
				dept_code.add(zip);
			}if(zip.getCode_type().equals("SKILL")){
				skill_code.add(zip);
			}else if(zip.getCode_type().equals("ACADEMIC")){
				academic_code.add(zip);
			}
		}
		
		mv.addObject("sex_code", sex_code);
		mv.addObject("dept_code", dept_code);
		mv.addObject("skill_code", skill_code);
		mv.addObject("academic_code", academic_code);
		
		
		return mv;
	}
	
	@RequestMapping("/modifyEmp.do")
	ModelAndView modifyEmp(EMP1010VO emp1010vo){
		Logger log=Logger.getLogger(this.getClass());
		log.debug("=======================>  "+emp1010vo);
		
		
		ModelAndView mv=doInitMain();
		
		if(!emp1010svc.setEmployee(emp1010vo)){
			mv.addObject("errMsg", emp1010svc.getErrMsg());
		}else{
			mv.addObject("successMsg","수정이 완료되었습니다.");
			
		}
		
		mv.setViewName("EMP1020P0");
		mv.addObject("employee_id", emp1010vo.getEmployee_id());
		mv.addObject("viewBean",emp1010svc.doMakeView(emp1010vo));
		return mv;
	}
	@RequestMapping("/deleteEmp.do")
	ModelAndView deleteEmp(EMP1010VO emp1010vo){
		Logger log=Logger.getLogger(this.getClass());
		log.debug("deleteEmp"+emp1010vo);
		
		ModelAndView mv=doInitMain();
		
		emp1010svc.delEmployee(emp1010vo.getEmployee_id());
		mv.addObject("viewBean",emp1010svc.doMakeView(emp1010vo));
		mv.setViewName("EMP1020P0");
		
		return mv;
	}
	
	@RequestMapping("/insertEmp.do")
	ModelAndView insEmp(EMP1010VO emp1010vo){
		Logger log=Logger.getLogger(this.getClass());
		log.debug("insertEmp"+emp1010vo);
		
		ModelAndView mv=doInitMain();
		
		if(!emp1010svc.insEmployee(emp1010vo)){
			mv.addObject("errMsg", emp1010svc.getErrMsg());
		}else{
			mv.addObject("successMsg","등록이 완료되었습니다.");
			
		}
		mv.setViewName("EMP1010P0");
		mv.addObject("viewBean",emp1010svc.doMakeView(emp1010vo));
		
		return mv;
	}
	
	
}
